package com.erbao.dp.flyweight;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

class Bullet {
    String id;
    boolean living;

    public Bullet() {
        id = UUID.randomUUID().toString();
        living = false;
        System.out.println("产生bullet，id："+id);
    }

    public boolean isLiving() {
        return living;
    }

    public void setLiving(boolean living) {
        this.living = living;
    }

    @Override
    public String toString() {
        return "bullet{" +
                "id='" + id + '\'' +
                '}';
    }
}

class BulletPool {
    List<Bullet> bulletList = new LinkedList<>();
    {
        for (int i = 0; i < 5; i++) {
            bulletList.add(new Bullet());
        }
    }

    public Bullet getBullet() {
        for (Bullet bullet : bulletList) {
            if (!bullet.isLiving()) {
                bullet.setLiving(true);
                return bullet;
            }
        }

        return new Bullet();
    }
}

/**
 * @author CaoBaoYin
 * @since 21/9/16 17:22
 */
public class Main {

    public static void main(String[] args) {
        BulletPool bp = new BulletPool();
        for (int i = 0; i < 10; i++) {
            System.out.println(bp.getBullet());
        }
    }

}
